'use strict'

const Controller = require('egg').Controller

class CommentController extends Controller {

    async findComment () {
        const { ctx } = this
        const { reply_id } = ctx.query

        const docs = await ctx.model.Comment.find({ $and:[{ reply_id },{ type:'father' }] })
            .populate({
                path:'user_id',
                select:{ nickname:1,avatar:1 }
            })
            .populate({
                path:'Child.user',
                select:{ nickname:1,avatar:1 }
            })
            .populate({
                path:'Child.comment'
            })
            .populate({
                path:'Child.reply_user_id',
                select:{ nickname:1 }
            })

        ctx.body = {
            state:'ok',
            data:docs
        }
    }

    async featuredComment () {
        const { ctx } = this
        const { reply_id } = ctx.query

        const docs = await ctx.model.Comment.find({ $and:[{ reply_id },{ type:'father' }] })
            .populate({
                path:'user_id',
                select:{ nickname:1,avatar:1 }
            })
            .sort({ like_count:1 })
            .limit(4)


        ctx.body = {
            state:'ok',
            data:docs
        }
    }

    //reply_user_id 回复的人id
    async Create () {
        const { ctx } = this
        const { question_id,reply_id,content,type,Father_id,reply_user_id,Child_id } = ctx.request.body

        let docs

        if (type === 'father') {
            docs = await ctx.model.Comment.create({
                user_id:ctx.user._id,
                question_id,
                reply_id,
                content,
                type
            })
        } else {
            docs = await ctx.model.Comment.create({
                user_id:ctx.user._id,
                question_id,
                reply_id,
                content,
                Father_id,
                type
            })
            // 更新回复的人的信息
            await ctx.model.Comment.updateOne({ _id:Father_id },{
                $push:{ Child:{ user:ctx.user._id,comment:docs._id,reply_user_id } }
            })

        }


        await ctx.model.Reply.updateOne({ _id:reply_id },{
            $inc:{ comment_count:1 },
            $push:{ comment:{ user:ctx.user._id,comment:docs._id } }
        })

        ctx.body = {
            state:'ok',
            data:docs
        }

        //自己回复自己不执行
        if (ctx.user._id.toString() === reply_user_id.toString()) {
            return
        }
        await ctx.model.Notice.create({
            user_id:reply_user_id,
            type:'消息',
            text:type === 'father' ? '回复了你的回答' : '回复了你的评论',
            question_id,
            res_comment_id:docs._id,
            res_user_id:ctx.user._id,
            comment_id:type === 'father' ? (Father_id ? Father_id : undefined) : (Child_id ? Child_id : Father_id),
            reply_id
        })
        ctx.service.notice.emit(reply_user_id)
    }

    async Like () {
        const { ctx } = this
        const { comment_id,type } = ctx.request.body

        let docs
        if (type === 'up') {
            docs = await ctx.model.Comment.findOneAndUpdate({ _id:comment_id },{
                $inc:{ like_count:1 },
                $push:{ like_id:ctx.user._id }
            },{ new:true })
            if (ctx.user._id.toString() !== docs.user_id.toString()) {
                ctx.model.Notice.create({
                    user_id:docs.user_id,
                    type:'赞同',
                    text:'点赞了你的评论',
                    res_user_id:ctx.user._id,
                    question_id:docs.question_id,
                    reply_id:docs.reply_id,
                    comment_id:docs._id
                })
                ctx.service.notice.emit(docs.user_id)
            }
        } else {
            docs = await ctx.model.Comment.findOneAndUpdate({ _id:comment_id },{
                $inc:{ like_count:-1 },
                $pull:{ like_id:ctx.user._id }
            },{ new:true })
        }

        ctx.body = {
            state:'ok',
            data:docs
        }

    }
}

module.exports = CommentController
